'''
商户管理相关测试
'''
from selenium.webdriver.common.by import By

from Lib.base.page_object.business.business_add import BusinessAdd
from Lib.base.page_object.business.business_list import BusinessList
from Lib.base.page_object.business.business_case import BusinessCase
from Lib.base.page_object.business.task import Task
import allure
from Lib.common.assert_class import AssertClass
import pytest
from Lib.common.get_data_by_mysql import MysqlAssert


@pytest.fixture(scope='class', autouse=True)
@pytest.mark.usefixtures('login')
def set_cls(login):
    TestBusiness.assertClass = AssertClass(login)
    TestBusiness.businessAdd = BusinessAdd(login)
    TestBusiness.businessList = BusinessList(login)
    TestBusiness.businessCase = BusinessCase(login)
    TestBusiness.task = Task(login)


@allure.feature("沿街商户管理测试")
class TestBusiness:

    @allure.title('新建商户')
    def test_01_business_add(self, login):
        '''
        现建商户页面测试开始
        :return:
        '''
        # 调用页面操作方法
        assert_args = self.businessAdd.business_add()
        # 断言
        self.assertClass.make_assert_by_text(args=assert_args, assert_data='新建商户成功', context="新建商户")

    @allure.title("商户列表筛选")
    def test_02_business_list(self, login):
        '''
        商户列表筛选
        :return:
        '''
        # 调用页面操作
        assert_args = self.businessList.business_list()
        # 断言设置
        self.assertClass.make_assert_by_text(args=assert_args, assert_data="5", context="商户违法次数")

    @allure.title("搜索商户、查看详情并关联视频源")
    def test_03_business_search_detail(self, login):
        '''
        搜索商户、查看详情并关联视频源
        :return:
        '''
        assert_args = self.businessList.business_search_and_detail()
        # 断言设置
        self.assertClass.make_assert_by_text(args=assert_args, assert_data="关联视频源成功", context="查看商户详情")

    @allure.title("编辑商户")
    @pytest.mark.skip
    def test_04_business_search_update(self, login):
        '''
        搜索商户、查看详情并修改
        :return:
        '''
        assert_args = self.businessList.business_search_and_update()
        # 断言设置
        self.assertClass.make_assert_by_text(args=assert_args, assert_data="编辑商户成功", context="编辑商户")

    @allure.title("删除商户")
    @pytest.mark.skip
    def test_05_business_search_delete(self, login):
        '''
        搜索商户、查看详情并修改
        :return:
        '''
        assert_args = self.businessList.business_search_and_delete()
        # 断言设置
        self.assertClass.make_assert_by_text(args=assert_args, assert_data="商户删除成功", context="删除商户")

    @allure.title('新建商户关联的事件')
    def test_07_business_create_case(self, login):
        '''
        商户关联事件页面测试
        :return:
        '''
        self.businessCase.business_case()
        assert_args = (By.XPATH, '//p[contains(text(), "事件上报成功")]')
        self.assertClass.make_assert_by_text(args=assert_args, assert_data='事件上报成功')

    @allure.title('查看商户关联的事件列表')
    def test_08_business_case(self, login):
        '''
        商户关联事件页面测试
        :return:
        '''
        with allure.step("事件列表筛选"):
            self.businessCase.business_case_list()
        with allure.step("事件列表搜索和排序"):
            self.businessCase.business_case_search_sort()
            assert_args = (By.XPATH, '//span[text()="商户信息"]')
        # 断言设置
        self.assertClass.make_assert_by_text(args=assert_args, assert_data="商户信息", context="商户事件列表")

    @allure.title('查看与商户相关联的事件')
    def test_09_business_search_and_detail(self, login):
        '''
        商户关联事件页面测试
        :return:
        '''
        with allure.step("查看当前列表的所有事件是否和商户相关"):
            self.businessCase.business_case_check()
            assert_args = (By.XPATH, '//tbody/tr/td[2]//span')
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data='市容')
        with allure.step("查看商户详情是否包含商户信息"):
            self.businessCase.business_search_and_detail()
            assert_args = (By.XPATH, '//div[text()="测试新建商户_请勿删除"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='测试新建商户_请勿删除')

    @allure.story("执法任务")
    @allure.title("新建商户执法任务")
    def test_create_business_task(self, login):
        with allure.step('新建任务'):
            self.task.create_task()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已下达"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已下达')

    @allure.story("执法任务")
    @allure.title("商户执法任务筛选列表")
    def test_business_site_list(self, login):
        with allure.step('根据任务来源进行筛选'):
            self.task.task_list_by_source()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr/td[8]//span')
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data='系统新建')
        with allure.step('根据任务阶段进行筛选'):
            self.task.task_list_by_stage()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr/td[6]//span')
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data='受理')
        with allure.step('根据任务状态进行筛选'):
            self.task.task_list_by_status()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr/td[7]//span')
            self.assertClass.make_assert_by_elements_text(args=assert_args, assert_data='已下达')

    @allure.story("执法任务")
    @allure.title("商户执法任务列表排序")
    def test_business_task_sort(self, login):
        self.task.task_list_sort()
        # 设置断言
        assert_args1 = (By.XPATH, '//tbody//tr[1]/td[3]//span')
        assert_args2 = (By.XPATH, '//tbody//tr[2]/td[3]//span')
        self.assertClass.make_assert_by_compare_time(args1=assert_args1, args2=assert_args2)

    @allure.story("执法任务")
    @allure.title("商户执法任务搜索")
    def test_business_task_search(self, login):
        with allure.step('错误搜索'):
            self.task.task_search_error()
            # 设置断言
            assert_args = (By.XPATH, '//p[contains(text(), "暂无数据")]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='暂无数据')
        with allure.step('按任务编号和执法对象进行搜索'):
            self.task.task_search()
            # 设置断言
            assert_args = (By.XPATH, '//tbody//tr[1]/td[4]//span')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='测试新建')

    @allure.story("执法任务")
    @allure.title("商户执法任务受理")
    def test_business_task_flow_accept(self):
        with allure.step('执法任务受理'):
            self.task.task_detail_accept()
            assert_args = (By.XPATH, '//div[@title="已受理"]//div[text()="已受理"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已受理')

    @allure.story("执法任务")
    @allure.title("商户执法任务检查")
    def test_business_task_flow_inspect(self):
        with allure.step('执法任务检查'):
            self.task.task_detail_inspect()
            assert_args = (By.XPATH, '//div[@title="已检查"]//div[text()="已检查"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已检查')

    @allure.story("执法任务")
    @allure.title("商户执法任务复查")
    def test_business_task_flow_review(self):
        with allure.step('执法任务复查'):
            self.task.task_detail_review()
            assert_args = (By.XPATH, '//div[@title="已复查"]//div[text()="已复查"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已复查')

    @allure.story("执法任务")
    @allure.title("商户执法任务归档")
    def test_business_task_flow_pigeonhole(self):
        with allure.step('执法任务归档'):
            self.task.task_detail_pigeonhole()
            assert_args = (By.XPATH, '//div[@title="已归档"]//div[text()="已归档"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已归档')

    @allure.story("执法任务")
    @allure.title("商户任务退单")
    def test_business_task_chargeback(self):
        with allure.step('新建执法任务'):
            self.task.create_task()
            # 设置断言
            assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已下达"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已下达')
        with allure.step('执法任务检查'):
            self.task.task_chargeback()
            assert_args = (By.XPATH, '//div[@class="global-message-card"]//div[text()="已退单"]')
            self.assertClass.make_assert_by_text(args=assert_args, assert_data='已退单')


if __name__ == '__main__':
    pytest.main(
        ['-v', '-s', '--reruns=1', '--reruns-delay=2',
         './business_test.py::TestBusiness::test_09_business_search_and_detail'])
    # pytest.main(['-v', './business_test.py', '--alluredir', '../result', '--clean-alluredir'])
    # os.system(r'copy ..\config\environment.properties ..\result\environment.properties')
    # os.system('allure generate  ../result -o ../report/ --clean')
